<?php
/**
 * Created by PhpStorm.
 * User: 宇
 * Date: 2016/10/7
 * Time: 9:41
 * Code 类
 */
namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Session;
use app\admin\model\Code;

class Codes extends Controller
{
    /*
     * 新增注册码
     */
    public function addCode(Request $request)
    {
        if(!(Session::has('username'))) {
            return 10005;
        } else {
            if(input('?post.dosubmit')){
                $ori_code = $request -> post('code');
                $ori_code_name = $request -> post('code_name');
                $input_val = new \app\common\controller\Index();
                $code = $input_val -> validateInput($ori_code);
                $code_name = $input_val -> validateInput($ori_code_name);
                $data['code'] = md5($code);
                $data['code_name'] = $code_name;
                //插入数据库
                $excute = model('Code') -> isUpdate(false) ->save($data);
                if ($excute) {
                    $this->success('新增成功!');
                } else {
                    $this->error();
                }
            } else {
                $this->view->replace([
                    '__PUBLIC__'    =>  '../../static/',
                    '__ROOT__'      =>  '/daogou/public/'
                ]);
                return $this->fetch('codes/codeAdd');
            }
        }
        return 0;
    }
    /*
     * 更新注册码
     */
    public function updateCode(Request $request)
    {
        if(!(Session::has('username'))) {
            return 10005;
        } else {
            $ori_code = $request -> post('new_code');
            $input_val = new \app\common\controller\Index();
            $val_code = $input_val -> validateInput($ori_code);
            //将过滤后的注册码存入数据库
            $md5_code = md5($val_code);
            $where['Id'] = $request -> post('code_id');
            $session_id = Session::get('code_id_'.$where['Id']);
            if ($session_id !== $where['Id']){
                return 0;
            }
            $data['code'] = $md5_code;
            $excute = model('Code') -> isUpdate(true) -> save($data, $where);
            if ($excute) {
//                $this->success('修改成功!');
                return 1;
            }
        }
        return 0;
    }
    /*
     * 删除注册码
     */
    public function delCode(Request $request)
    {
        if(!(Session::has('username'))) {
            return 10005;
        } else {
            $where['Id'] = $request -> post('code_id');
            //检查是否真实通过验证
            $session_id = Session::get('code_id_'.$where['Id']);
            if ($session_id !== $where['Id']){
                return 0;
            }
            //删除数据库内容
            $excute = model('Code') -> where($where) -> delete();
            if (!empty($excute)){
                return 1;
            } else {
                return 0;
            }
        }
    }

    /*
     * 注册码列表
     */
    public function codeList()
    {
        if(!(Session::has('username'))) {
            return 10005;
        } else {
            //获取数据库数据
            $code_obj = Code::all();
            //dump($code_obj);
            $code_arr = array();
            foreach ($code_obj as $k => $v) {
                $code_arr[$k] = $v -> toArray();
            }
//            dump($code_arr);
            $this -> assign('code_list', $code_arr);
            $this->view->replace([
                '__PUBLIC__'    =>  '../../static/',
                '__ROOT__'      =>  '/daogou/public/'
            ]);
            return $this->fetch('codeShow');
        }
    }

    /*
     * 验证原码ajax
     */
    public function codecheckAjax(Request $request)
    {
        if(!(Session::has('username'))) {
            return 10005;
        } else {
            $check_code = $request -> post('check_code');
            $code_id = $request -> post('code_id');
            //dump($check_code);
            if ('' == $check_code){
                return 2;
            }
            $input_val = new \app\common\controller\Index();
            $check_code = $input_val -> validateInput($check_code);
            //与数据库核对
            $where['code'] = md5($check_code);
            //dump($where);
            $excute = Code::where($where) -> find();

            if(empty($excute)){
                return 0;
            } else {
                //加入session
                Session::set('code_id_'.$code_id, $code_id);
                return 1;
            }
        }
    }
}